define(["angularAMD"], function (angularAMD) {
        angularAMD.service('B0210Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$calculater','$mddProvider',
            function($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater,$mddProvider){
                //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                this.init = function(options,selectedEquipmentList,feedback){
                  
                    var machine_no = options ? options.machine_no : "",  // 20240105 Modify by shiwl	0155515: 作业平台全选设备点检时无法弹出点检界面
                        machine_display =  options ? options.machine_display : "",
                        machineCheckList = {},
                        checklist_no,
                        checklist_display;

                      
                    //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                   
                    var selected_EquipmentList = selectedEquipmentList;
                    // 取得點檢頻率
                    var equipment_inspection_frequency_get = function(){
                        var qc_frequency;
                        var equipmentcheckuprate;
                        equipment_inspection_frequency_info_get(function(itemList){
                            if(itemList && itemList.length > 0){
                                //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                                equipmentcheckuprate = $filter('translate')('common.msg.plz_select');
                                qc_frequency = itemList[0].qc_frequency;
                            }
                            getCheckList(qc_frequency,equipmentcheckuprate);
                        });
                    };
    
                    function equipment_inspection_frequency_info_get(callback){
                        $MMWService.pageNumberInfoGet({
                            form_name:"PageNo_61",
                            query_condition_info:[
                                {column_name:'ra.EquipmentNo', query_mode:'0', value: machine_no, merge_condition_model:'AND'}
                            ]
                        }, function(itemList){
                            if(callback)
                                callback(itemList);
                        });
                    };
                    
                    // 取得點檢表
                    var getCheckList = function(qc_frequency,equipmentcheckuprate){
                        $MMWService.sendToServer({
                            //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                            uri: 'ServicesSTD.Module_EQP.machine_checklist_get',
                            content: {
                                machine_no: machine_no,
                                qc_frequency: qc_frequency,
                                selected_EquipmentList: selected_EquipmentList,
                            },
                            //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                            success: function (data) {
                                if (data.check_msg) {
                                    $rootScope.showAlert($filter('translate')('B0210.msg.not_set_up_QClist') + data.check_msg);
                                //20210605 Modify by德義家興for#91011: 有檢查到問題就不開窗
                                }else if (data.machine_checklist_get_detail && data.machine_checklist_get_detail.length > 0) {                                checklist_no = data.machine_checklist_get_detail[0].checklist_no;
                                    checklist_display = $filter('translate')('common.msg.plz_select');
                                    machineCheckList = data.machine_checklist_get_detail[0].machine_checklist_get_checklist_detail;
                                    //20210610 Modify by德義家興for#91011:
                                    defaulCheckUpRate = machineCheckList[0].equipmentcheckuprate;
                                    equipmentcheckuprate = $filter('translate')('B0210.checkUprate.'+defaulCheckUpRate);
                                    checklist_display = machineCheckList[0].checklist_name + "(" + machineCheckList[0].checklist_no + ")";
                                    function filterForDefaulCheckList(item) {
                                        if (item.equipmentcheckuprate == defaulCheckUpRate) {
                                          return true;
                                        }else{
                                          return false;
                                        }
                                    }
                                    defaulCheckList = machineCheckList.filter(filterForDefaulCheckList);
                                //     defaulCheckList.forEach(item=>{
                                //         item.check_valueD = "";
                                //     })
                                    openDialog(machineCheckList, qc_frequency, equipmentcheckuprate, defaulCheckList);
                                }
                            }
                        });
                    };
    
                    // 更換點檢表
                    var changeCheckList = function(qc_frequency, callback){
                        $MMWService.sendToServer({
                           uri: 'ServicesSTD.Module_EQP.machine_checklist_get',
                            content : {
                                machine_no : machine_no,
                                qc_frequency : qc_frequency,
                                //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                                selected_EquipmentList: selected_EquipmentList,
                            },
                            success : function(data){
                                if(callback)
                                    callback(data);
                            }
                        });
                    };
    
                    // 取得設備集成採集參數
                    function machine_parameter_info_get(check_item){
                        $MMWService.sendToServer({
                           uri: 'ServicesSTD.Module_EQP.machine_parameter_info_get',
                            content : {
                                equipment_no : machine_no,
                                machine_parameters:[
                                    {parameter_no:check_item.param_no}
                                ]
                            },
                            success : function(data){
                                if(data.machine_checklist_get_checklist_detail.length>0){
                                    check_item.check_value = data.machine_checklist_get_checklist_detail[0].value;
                                }
                            }
                        });
                    }
                  
                    var openDialog = function(machineCheckList, qc_frequency, equipmentcheckuprate, defaulCheckList){
                        $mddProvider.UseForm('B0210', () => {
                                $mdDialog.dialog(
                                        'module/moduleB/program/B02/B0210/B0210.html',
                                        function(dialog){
                                            return {
                                                _title:selectedEquipmentList.length> 1 ? $filter('translate')('B0210.titleMultiple'): $filter('translate')('B0210.title'),
                                                css_url:'module/moduleB/program/B02/B0210/B0210.css',
                                                machine_no: machine_no,
                                                machine_display: machine_display,
                                                checklist_no: checklist_no,
                                                checklist_display: checklist_display,
                                                machineCheckList: machineCheckList,
                                                equipmentcheckuprate: equipmentcheckuprate,
                                                qc_frequency: qc_frequency,
                                                //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                                                selectedEquipmentList: selectedEquipmentList,
                                                machineCheckList_display: defaulCheckList, //下拉選單篩選出來的list;顯示用
                                                _event: {
                                                    'equipmentcheckuprate': {
                                                       click: () => {
                                                               dialog.getEquipmentCheckUprate();
                                                       },
                                                       isDisabled: 'Y'
                                                            
                                                    },
                                                //     'dialog.B0210_Table.check_value':{
                                                //         click: (item) => {
                                                //                 dialog.calculater(item,true);
                                                //         }
                                                //     }
                                                },
                                                _fn:{
                                                   'Execute':{
                                                        click: () => {
                                                                dialog.confirm();
                                                        }
                                                   }
                                                },
                                                getEquipmentCheckUprate: function(){ //下拉選單;取得點檢頻率
                                                    $MMWService.showQueryListPNIG({
                                                        page_mode: 'equipment_inspection_frequency_get',
                                                        header: [
                                                            {label:$filter('translate')('B0210.equipmentCheckUprate'), code:'equipmentcheckuprate'},
                                                            {label:$filter('translate')('B0210.qcListNo'), code:'qclistno'},
                                                            {label:$filter('translate')('B0210.qcListName'), code:'qclistname'},
                                                        ],
                                                        setCondition:function(conditions){
                                                            conditions.push({column_name:'ra.EquipmentNo', query_mode:'0', value: machine_no, merge_condition_model:'AND'});           
                                                            return conditions;
                                                        },
                                                        selectCode: '',
                                                        confirm : function(selectItem){
                                                            dialog.equipmentcheckuprate = selectItem.equipmentcheckuprate;
                                                            dialog.qc_frequency = selectItem.qc_frequency;
                                                            changeCheckList(dialog.qc_frequency, function(data){
                                                                dialog.checklist_no = data.machine_checklist_get_detail[0].checklist_no;
                                                                dialog.checklist_display = data.machine_checklist_get_detail[0].checklist_display;
                                                                dialog.machineCheckList = data.machine_checklist_get_detail[0].machine_checklist_get_checklist_detail;
                                                                dialog.machineCheckList_display = data.machine_checklist_get_detail[0].machine_checklist_get_checklist_detail;
                                                                //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                                                             //   dialog.B0210_Table = data.machine_checklist_get_detail[0].machine_checklist_get_checklist_detail;
                                                            });
                                                        }
                                                    })
                                                },
                                                //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                                                getMultiSelectedEquipmentCheckUprate:function(){
                                                    //下拉選單
                                                    var list = [];
                                                    machineCheckList.forEach(function(item){
                                                        list.push({
                                                            "equipmentcheckuprate": $filter('translate')('B0210.checkUprate.'+item.equipmentcheckuprate), 
                                                            "qclistno": item.checklist_no, 
                                                            "qclistname": item.checklist_name,
                                                            "qc_frequency": item.equipmentcheckuprate,
                                                        });
                                                    });
                                                    //去除重複
                                                    list = [...new Set(list.map(item => JSON.stringify(item)))].map(item => JSON.parse(item));
                                                    $rootScope.showQueryList({
                                                        title: $filter('translate')('common.msg.plz_select'),
                                                        header : [
                                                            {label:$filter('translate')('B0210.equipmentCheckUprate'), code:'equipmentcheckuprate'},
                                                            {label:$filter('translate')('B0210.qcListNo'), code:'qclistno'},
                                                            {label:$filter('translate')('B0210.qcListName'), code:'qclistname'},
                                                        ],
                                                        query: function(conditions, feedback){
                                                            feedback(list);
                                                        },
                                                        confirm: function(selectItem, Dialog){
                                                            //下拉選單
                                                            dialog.equipmentcheckuprate = selectItem.equipmentcheckuprate;
                                                            dialog.qc_frequency = selectItem.qc_frequency;
                                                            //label
                                                            dialog.checklist_no = selectItem.qclistno;
                                                            dialog.checklist_display = selectItem.qclistname + "(" + selectItem.qclistno + ")";
                                                            //grid
                                                            function filterByEquipmentcheckuprate(item) {
                                                                if (item.equipmentcheckuprate == selectItem.qc_frequency) {
                                                                  return true;
                                                                }else{
                                                                  return false;
                                                                }
                                                            }
                                                            dialog.machineCheckList_display  = dialog.machineCheckList.filter(filterByEquipmentcheckuprate);
                                                            Dialog.hide();
                                                        }
                                                    })
                                                },
                                                // 20220214 modify by Alan for#102689 : 新增負數功能
                                                calculater: function(item, enableNegative){
                                                    var target = event.currentTarget;
                                                    item.calculaterInput = true;
                                                    angular.element(target).addClass('is-focus');
                                                    $calculater.open(function (num, dialogType) {
                                                        item.check_value = num;
                                                        angular.element(target).removeClass('is-focus');
                                                        item.calculaterInput = false;
                                                        dialogType.hide();
                                                    },
                                                    function (dialogType) {
                                                        angular.element(target).removeClass('is-focus');
                                                        item.calculaterInput = false;
                                                        dialogType.hide();
                                                    },
                                                    {
                                                        defaultNum: item.check_value,
                                                        decimal: true,
                                                        enableNegative: enableNegative
                                                    });
                                                },
                                                itemStatus : function(item){
                                                   if(!item){
                                                        return '';
                                                   }else {
                                                        if(item.check_type == '1'){
                                                                if(item.std_value == item.check_value){
                                                                    item.check_result = '0'; //ok 0
                                                                    return 'check-point-ok';
                                                                } else {
                                                                    item.check_result = '1'; //ng 1
                                                                    return 'check-point-notok';
                                                                }
                                                            } else if(item.check_type == '2'){
                                                                var isInterval = true;
                                                                //M#85636 修正點檢項目區間為 0 以上時 沒輸入值 卻是合格狀態
                                                                if(item.check_value == '' || item.check_value == null)
                                                                    isInterval=false;
                                                                if(item.range_min !== null){
                                                                    isInterval = isInterval && ((+item.range_min) <= (+item.check_value));
                                                                }
                                                                if(item.range_max !== null){
                                                                    isInterval = isInterval && ((+item.check_value) <= (+item.range_max));
                                                                }
                        
                                                                if(item.check_value == '' || item.check_value == null) isInterval=false;
                        
                                                                if(isInterval){
                                                                    item.check_result = '0'; //ok 0
                                                                    return 'check-point-ok';
                                                                } else {
                                                                    item.check_result = '1'; //ng 1
                                                                    return 'check-point-notok';
                                                                }
                                                            }
                                                            return '';
                                                   }
                                                   
                                                },
                                                type3Init : function(item){
                                                   if(!item){
                                                    return ''
                                                   } else{
                                                        if(!item.check_value){
                                                                item.check_result = '1';
                                                              //  item.check_value = 2;
                                                         }
                                                   }
                                                    
                                                },
                                                itemClick : function(item){
                                                    item.isClick = !item.isClick;
                                                    if(item.check_value == '1'){
                                                        item.check_result = '1' ; //ng 1
                                                        item.check_value = 2;
                                                    } else {
                                                        item.check_result = '0'; //ok 0
                                                        item.check_value = 1;
                                                    }
                                                },
                                                get_check_value:machine_parameter_info_get,
                                                back: function () {
                                                    dialog.hide();
                                                    if(options.back)
                                                        options.back();
                                                },
                                                confirm: function () {
                                                    var msg = '';
                                                    var checklist_detail = [];
                                                    //20210605 Modify by德義家興for#91011: 修改卡控的目標為dialog.machineCheckList_display
                                                    dialog.machineCheckList_display.forEach(function(item){
                                                        if(item.check_type == '1' || item.check_type == '2'){
                                                            if(item.check_value == undefined || item.check_value === ''){
                                                                msg += $filter('translate')('B0210.no') + item.item_no
                                                                    + $filter('translate')('B0210.msg.check_value_empty')+'<br>';
                                                            }
                                                        }
                                                        //20210612 add by德義家興for#91011:排除check_value是null時check_value,check_result沒給值的問題
                                                        if (item.check_type == '3' && item.check_value == null) {
                                                            item.check_result = '1' ;
                                                            item.check_value = 2;
                                                        }
                                                        // 20231206 modify by wenwei for M#152075: 【衡川科技】【正式区】设备点检文本类的填写数据显示点检不通过
                                                        if (item.check_type == '4') item.check_result = ''
                                                        //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                                                        if (dialog.qc_frequency == item.equipmentcheckuprate) {
                                                            var detail = {
                                                                item_no:item.item_no,
                                                                check_value:item.check_value||'',
                                                                check_result:item.check_result||''
                                                            };
                                                            checklist_detail.push(detail);    
                                                        }
                                                    });
                                                    if(msg == ''){
                                                        var content = {
                                                            machine_no : dialog.machine_no,
                                                            checklist_no : dialog.checklist_no,
                                                            checklist_detail: checklist_detail,
                                                            qc_frequency: dialog.qc_frequency,
                                                            //20210506 Modify by德義家興for#91011: [标准][DEV][行业议题]机加工-CNC-批量设备点检
                                                            selected_EquipmentList: selected_EquipmentList
                                                        }
                
                                                        //20180521 modify by 李栋 for mantis#0047128
                                                        $MMWService.sendToServer({
                                                            uri: 'ServicesSTD.Module_EQP.exe_machine_check_process',
                                                            content : content,
                                                            success : function(data){
                                                                //20180416 modify by 李栋 for mantis#0045407
                                                                $rootScope.showToast($filter('translate')('B0210.showAlert'));
                                                                dialog.hide();
                                                                if(feedback)
                                                                    feedback();
                                                            }
                                                        });
                                                    } else {
                                                        $rootScope.showAlert(msg);
                                                    }
                                                }
                                            }
                                        }
                                    );

                        })
                       
                    }
    
                    equipment_inspection_frequency_get();
                };
            }]);
    });
    